﻿using BW.EatRabbit.API.Application.Commands.Breeding;
using BW.EatRabbit.API.Dtos;
using BW.EatRabbit.Domain;
using BW.EatRabbit.ErrorCode;
using BW.EatRabbit.Infrastructure.Interfaces;
using MediatR;

namespace BW.EatRabbit.API.Application.CommandHandlers.Breeding
{
    public class GetBasicInformationQueryCommadHandler : IRequestHandler<GetBasicInformationQueryCommad, APIResult<BasicDTO>>
    {
        private readonly IBaseRepository<BasicInformation> _basicinformationRepository;
        private readonly IBaseRepository<Goods> _goodsRepository;
        public GetBasicInformationQueryCommadHandler(IBaseRepository<BasicInformation> basicinformationRepository, IBaseRepository<Goods> goodsRepository)
        {
            _basicinformationRepository = basicinformationRepository;
            _goodsRepository = goodsRepository;
        }
        public Task<APIResult<BasicDTO>> Handle(GetBasicInformationQueryCommad request, CancellationToken cancellationToken)
        {
            APIResult<BasicDTO> result = new APIResult<BasicDTO>();
            var list = from a in _basicinformationRepository.GetAll()
                       join b in _goodsRepository.GetAll() on a.GoodsId equals b.GoodsId
                       where !a.IsDeleted
                       select new BasicDTO
                       {
                           BasicInforId = a.BasicInforId,
                           GoodsId = b.GoodsId,
                           GoodsName = b.GoodsName,
                           ProductionBatch = a.ProductionBatch,
                           ShelfLife = a.ShelfLife,
                           License = a.License,
                           Completeness = a.Completeness,
                           Producer = a.Producer,
                           CoverImage = a.CoverImage,
                           CreatedTime = a.CreatedTime,
                           Creator = a.Creator,
                           UpdatedTime = a.UpdatedTime,
                           Updator = a.Updator
                       };

            var entity = list.FirstOrDefault(x=>x.ProductionBatch==request.ProductionBatch);
             if(entity == null)
            {
                result.Code = ResultCode.查询失败;
                result.Message = ResultCode.查询失败.ToString() + ",没有该基本信息";
                return Task.FromResult(result);
            }
            result.Code = ResultCode.查询成功;
            result.Message = ResultCode.查询成功.ToString();
            result.Data = entity;

            return Task.FromResult(result);

        }
    }
}
